package com.longdaji.decoration.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.longdaji.decoration.R;
import com.longdaji.decoration.bean.Dynamic;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Kanade on 2018/2/27 0027.
 */

public class DynamicAdapter extends RecyclerView.Adapter<DynamicAdapter.DynamicHolder> {

    private List<Dynamic> mDynamicList;
    private Context mContext;

    public DynamicAdapter(List<Dynamic> dynamicList) {
        mDynamicList = dynamicList;
    }

    @Override
    public DynamicHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_dynamic, parent, false);
        return new DynamicHolder(view);
    }

    @Override
    public void onBindViewHolder(DynamicHolder holder, int position) {
        Dynamic dynamic = mDynamicList.get(position);
        Glide.with(mContext).load(dynamic.getUserPortraitUri()).into(holder.ivUserPortrait);
        holder.tvDynamicTime.setText(dynamic.getDynamicTime());
        holder.tvUserName.setText(dynamic.getUserName());
        holder.tvFollow.setText(dynamic.isFollow() ? "已关注" : "关注");
        holder.tvDynamicTitle.setText(dynamic.getDynamicTitle());
        Glide.with(mContext).load(dynamic.getDynamicContentUri()).into(holder.ivDynamicContent);
        holder.tvForwardCount.setText(dynamic.getForwardCount());
        holder.tvSupportCount.setText(dynamic.getSupportCount());
        holder.tvCommentCount.setText(dynamic.getCommentCount());

    }


    @Override
    public int getItemCount() {
        return mDynamicList.size();
    }

     class DynamicHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_user_portrait)
        ImageView ivUserPortrait;
        @BindView(R.id.tv_dynamic_time)
        TextView tvDynamicTime;
        @BindView(R.id.tv_user_name)
        TextView tvUserName;
        @BindView(R.id.tv_follow)
        TextView tvFollow;
        @BindView(R.id.tv_dynamic_title)
        TextView tvDynamicTitle;
        @BindView(R.id.iv_dynamic_content)
        ImageView ivDynamicContent;
        @BindView(R.id.tv_dynamic_forward)
        TextView tvForwardCount;
        @BindView(R.id.tv_dynamic_support)
        TextView tvSupportCount;
        @BindView(R.id.tv_dynamic_comment)
        TextView tvCommentCount;

        public DynamicHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }

        @OnClick({R.id.ll_forward, R.id.ll_support, R.id.ll_comment})
        void onItemViewClicked(View view) {
            switch (view.getId()) {
                case R.id.ll_forward:
                    Log.d("mytest", "forward clicked");
                    break;
                case R.id.ll_support:
                    Log.d("mytest", "support clicked");
                    break;
                case R.id.ll_comment:
                    Log.d("mytest", "comment clicked");
                    break;
                default:
                    break;
            }
        }
    }


    /*
    * ---------------------------
    * */
    private ForwardListener forwardListener;
    private SupportListener supportListener;
    private CommentListener commentListener;
    private FollowListener followListener;
    private UserListener userListener;

    /*
    * ---------------------------
    * */
    public interface ForwardListener {
        void onForwardClick();
    }

    public interface SupportListener {
        void onSupportClick();
    }

    public interface CommentListener {
        void onCommentClick();
    }

    public interface FollowListener {
        void onFollowClick();
    }

    public interface UserListener {
        void onUserClick();
    }

    /*
    * ---------------------------
    * */
    public void setForwardListener(ForwardListener listener) {
        forwardListener = listener;
    }

    public void setSupportListener(SupportListener listener) {
        supportListener = listener;
    }

    public void setCommentListener(CommentListener listener) {
        commentListener = listener;
    }

    public void setFollowListener(FollowListener listener) {
        followListener = listener;
    }

    public void setUserListener(UserListener listener) {
        userListener = listener;
    }


}
